<template>
  <div id="">
    <input type="file" name="" value="" @change='add_img'>
  </div>
</template>
<script>
export default {
  name: "",
  props: ['imgtype'],
  data: () => ({
    imgData: {
      accept: 'image/gif, image/jpeg, image/png, image/jpg',
    },
  }),
  methods: {
    // 上传图片
    add_img(event){
        let self = this;
        let reader =new FileReader();
        let img1=event.target.files[0];
        let type=img1.type;//文件的类型，判断是否是图片
        let size=img1.size;//文件的大小，判断图片的大小
        if(this.imgData.accept.indexOf(type) == -1){
            alert('请选择我们支持的图片格式！');
            return false;
        }
        if(size>3145728){
            alert('请选择3M以内的图片！');
            return false;
        }
        var uri = ''
        let form = new FormData();
        form.append('file',img1,img1.name);
        this.$ajax.post(configUrl.UPLOADFILES_URL,form,{
            headers:{'Content-Type':'multipart/form-data'}
        }).then(response => {
            console.log(response.data)
            uri = response.data.result
            reader.readAsDataURL(img1);

            reader.onloadend=function(){
              if (self.imgtype) {
                self.$emit('uploadImgEvent',{uri:uri,imgtype:self.imgtype});
              }else {
                self.$emit('uploadImgEvent',uri);
              }
            }
        }).catch(error => {
            alert('上传图片出错！');
        })
      },
  }
}
</script>
<style lang="scss" scoped>
input{
  width: 100%;
  height: 100%;
}
</style>
